package com.worldcup.service.question.impl;

import com.worldcup.dao.question.QuestionMatchResultMapper;
import com.worldcup.model.question.QuestionMatchResult;
import com.worldcup.service.question.QuestionMatchResultService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author duwan.zq
 * @date 2019/03/11
 */
@Service
public class QuestionMatchResultServiceImpl implements QuestionMatchResultService {

    //todo 动态计算？缓存计算？
    private final static int MAX_SCORE = 606;

    private final static int MIN_SCORE = -601;

    @Autowired
    private QuestionMatchResultMapper matchResultMapper;


    @Override
    public void insert(String openid, String passiveOpenid, BigDecimal harmonyScore, BigDecimal topicScore,
                       List<String> commonIdols, List<String> commonEnemies, List<String> commonMatches) {
        QuestionMatchResult questionMatchResult = buildMatchResult(openid, passiveOpenid, harmonyScore, topicScore, commonIdols, commonEnemies, commonMatches);
        matchResultMapper.insert(questionMatchResult);
    }

    @Override
    public QuestionMatchResult getMatchResult(String openid, String passiveOpenid) {
        return matchResultMapper.getMatchResult(openid, passiveOpenid);
    }

    @Override
    public List<QuestionMatchResult> getFriendRank(String openid, List<String> friendOpenids) {
        return matchResultMapper.getFriendRank(openid, friendOpenids);
    }

    @Override
    public void randomInterest(String openid) {
        //matchResultMapper.get9
        //    IK
    }

    @Override
    public List<QuestionMatchResult> getMatchResultExclude(String openid, List<String> friendOpenids) {
        return matchResultMapper.getMatchResultExclude(openid, friendOpenids);
    }

    private QuestionMatchResult buildMatchResult(String openid, String passiveOpenid, BigDecimal harmonyScore,
                                                 BigDecimal topicScore, List<String> commonIdols,
                                                 List<String> commonEnemies, List<String> commonMatches) {
        QuestionMatchResult questionMatchResult = new QuestionMatchResult();
        questionMatchResult.setOpenid(openid);
        questionMatchResult.setPassiveOpenid(passiveOpenid);
        questionMatchResult.setHarmonyScore(harmonyScore);
        questionMatchResult.setTopicScore(topicScore);
        questionMatchResult.setCommonIdol(StringUtils.join(commonIdols, ","));
        questionMatchResult.setCommonEnemy(StringUtils.join(commonEnemies, ","));
        questionMatchResult.setCommonMatch(StringUtils.join(commonMatches, ","));
        questionMatchResult.setAllowView(false);
        questionMatchResult.setCreatedAt(LocalDateTime.now());
        questionMatchResult.setUpdatedAt(LocalDateTime.now());
        return questionMatchResult;
    }


}
